package com.example.kingdee.service;

import com.example.kingdee.block.AllSunBlock;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.dto.AllSunDto;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GameLandService {

    @Autowired
    private SqlTool sqlTool;

    @Autowired
    private AllSunBlock allSunBlock;

    @Autowired
    private TBxStMapper tBxStMapper;

    /**
     * 判断生产订单物料是否齐套
     * @param allSunDto
     * @return
     */
    public String isComplete(AllSunDto allSunDto) {
        List<Map<String, Object>> paramList = allSunDto.getParamList();
        //将传过来的物料按照物料id进行需求量汇总
        Map<String, Double> topItemHavaQty = allSunBlock.getItemDemandQty(paramList);
        //计算子级物料需求量
        List<Map<String, Object>> postItemList = allSunBlock.calculatePostDemandQty(topItemHavaQty, allSunDto);
        //汇总子级物料需求量
        Map<String, Double> postItemHavaQty = allSunBlock.getItemDemandQty(postItemList);
        //判断物料即时库存量是否齐套并写入缓存表
        allSunBlock.isComplete(postItemHavaQty, allSunDto);
        return "200";
    }

    /**
     * 构建采购订单汇报数据
     * @param allSunDto
     * @return
     */
    public String reportData(AllSunDto allSunDto) {
        //查询采购订单信息
        List<Map<String, Object>> purEntryList = tBxStMapper.selectPurOrderEntry(allSunDto.getPurId());
        if (purEntryList != null && purEntryList.size() != 0){
            for (Map<String, Object> purEntryInfo : purEntryList) {
                purEntryInfo.put("userName", allSunDto.getUserName());
                Object itemTextureId = purEntryInfo.get("itemTextureId");
                Object itemGroupTypeId = purEntryInfo.get("itemGroupTypeId");
                Object nodeSort = purEntryInfo.get("nodeSort");
                if (nodeSort == null){
                    //查询当前分类的第一个节点
                    String nodeId = tBxStMapper.getGroupTop(itemTextureId, itemGroupTypeId);
                    if (!StringUtils.isEmpty(nodeId) && !nodeId.equals("680708dbfc41ee")){
                        purEntryInfo.put("nodeId", nodeId);
                        purEntryInfo.put("nodeSort", 1);
                    }else {
                        //填充无
                        purEntryInfo.put("nodeId", "680708dbfc41ee");
                        purEntryInfo.put("nodeSort", 1);
                    }
                }else {
                    //查询房钱分类的下一个节点
                    String nodeId = tBxStMapper.getGroupTopNext(itemTextureId, itemGroupTypeId, nodeSort);
                    if (!StringUtils.isEmpty(nodeId) && !nodeId.equals("680708dbfc41ee")){
                        purEntryInfo.put("nodeId", nodeId);
                        purEntryInfo.put("nodeSort", Integer.parseInt(nodeSort.toString()) + 1);
                    }else {
                        //填充完成
                        purEntryInfo.put("nodeId", "680708dbfc41ee");
                        purEntryInfo.put("nodeSort", 7);
                    }
                }
                //判断是否为未道节点
                String nodeId = tBxStMapper.getGroupTopNext(itemTextureId, itemGroupTypeId, purEntryInfo.get("nodeSort"));
                if (nodeId == null || nodeId.equals("680708dbfc41ee")){
                    purEntryInfo.put("isLast", "是");
                }else {
                    purEntryInfo.put("isLast", "否");
                }
                //保存数据
                tBxStMapper.insertPurData(purEntryInfo);
            }
        }
        return "200";
    }
}




























